﻿Public Class xtf_ABMconfig_parametro_ 
    Dim flg_Modificado As Boolean
    Dim entorno As String

    Private Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        Dim objParametroManejador As New cls_config_parametro_
        For Each objGeneral As DevExpress.XtraLayout.TabbedControlGroup In lcg_general.Items
            For Each objTab As DevExpress.XtraLayout.LayoutControlGroup In objGeneral.TabPages
                For Each objGrupo As DevExpress.XtraLayout.LayoutControlGroup In objTab.Items
                    For Each objNombre As DevExpress.XtraLayout.LayoutControlItem In objGrupo.Items
                        Dim objParametro As cls_config_parametro_ = objParametroManejador.Buscar(objNombre.Name)
                        If Not IsNothing(objParametro) Then
                            Select Case objParametro.tipo
                                Case cls_config_parametro_.parametro_tipo.chk
                                    objParametro.valor = CInt(CType(objNombre.Control, DevExpress.XtraEditors.CheckEdit).Checked)
                                Case cls_config_parametro_.parametro_tipo.ctm
                                    Select Case objParametro.nombre
                                        Case "menuPorDefecto"
                                            objParametro.valor = CType(objNombre.Control, DevExpress.XtraEditors.ComboBoxEdit).EditValue
                                        Case "interfaz"
                                            objParametro.valor = CInt(CType(objNombre.Control, DevExpress.XtraEditors.LookUpEdit).EditValue)
                                            objParametro.valores = CType(objNombre.Control, DevExpress.XtraEditors.LookUpEdit).Text
                                        Case Else
                                            objParametro.valor = CInt(CType(objNombre.Control, DevExpress.XtraEditors.LookUpEdit).EditValue)
                                    End Select

                                Case cls_config_parametro_.parametro_tipo.xmlReport
                                    objParametro.xml = (CType(objNombre.Control, DevExpress.XtraEditors.SimpleButton).Tag)
                                Case Else
                                    objParametro.valor = objNombre.Control.Text
                            End Select
                            objParametroManejador.Modificar(objParametro)
                        End If
                    Next
                Next
            Next
        Next

        'Ejecucion de script de cambio de entorno
        Dim objParametroEntorno As New cls_config_parametro_
        objParametroEntorno.Buscar("interfaz")
        If entorno <> objParametroEntorno.valor Then
            entorno = objParametroEntorno.valor
            Dim config_interfaz As New cls_config_interfaz_
            config_interfaz.Buscar(entorno)
            If cambiarEntorno(config_interfaz.script) Then
                ErrManagerShow("000013", Me)
            Else
                ErrManagerShow("000014", Me)
            End If
        End If

        btn_Guardar.Enabled = False
    End Sub

    Private Sub btn_restablecer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_restablecer.Click
        Dim objParametroManejador As New cls_config_parametro_
        For Each objGeneral As DevExpress.XtraLayout.TabbedControlGroup In lcg_general.Items
            For Each objTab As DevExpress.XtraLayout.LayoutControlGroup In objGeneral.TabPages
                For Each objGrupo As DevExpress.XtraLayout.LayoutControlGroup In objTab.Items
                    For Each objNombre As DevExpress.XtraLayout.LayoutControlItem In objGrupo.Items
                        Dim objParametro As cls_config_parametro_ = objParametroManejador.Buscar(objNombre.Name)
                        If Not IsNothing(objParametro) Then
                            Select Case objParametro.tipo
                                Case cls_config_parametro_.parametro_tipo.chk
                                    CType(objNombre.Control, DevExpress.XtraEditors.CheckEdit).Checked = CBool(objParametro.valorpredeterminado)
                                    objParametro.valor = CInt(CType(objNombre.Control, DevExpress.XtraEditors.CheckEdit).Checked)
                                Case Else
                                    objParametro.valor = objNombre.Control.Text
                                    objNombre.Control.Text = objParametro.valorpredeterminado
                            End Select
                            objParametroManejador.Modificar(objParametro)
                        End If
                    Next
                Next
            Next
        Next

    End Sub

    Public Sub New(ByVal prm_int_tipo2 As cls_config_parametro_.parametro_tipo2)

        ' This call is required by the designer.
        InitializeComponent()
        Select Case prm_int_tipo2
            Case cls_config_parametro_.parametro_tipo2.general
                Me.Text = "Parámetros"
            Case cls_config_parametro_.parametro_tipo2.informes
                Me.Text = "Configuración de Informes"
        End Select

        lcg_general.BeginUpdate()
        Dim objParametroManejador As New cls_config_parametro_
        Dim objGeneral As DevExpress.XtraLayout.TabbedControlGroup = lcg_general.AddTabbedGroup()
        For Each strTab As String In objParametroManejador.tab_(prm_int_tipo2)
            Dim objTab As DevExpress.XtraLayout.LayoutControlGroup = objGeneral.AddTabPage
            objTab.Text = strTab
            For Each strGrupo As String In objParametroManejador.grupo_(strTab, prm_int_tipo2)
                Dim objGrupo As DevExpress.XtraLayout.LayoutControlGroup = objTab.AddGroup
                objGrupo.Text = strGrupo
                For Each objParametro As cls_config_parametro_ In objParametroManejador.cargarXTabXGrupo(strTab, strGrupo, prm_int_tipo2)
                    If objParametro.visible Or UsuarioLOG.esroot Then
                        Dim objNombre As DevExpress.XtraLayout.LayoutControlItem = objGrupo.AddItem
                        objNombre.Name = objParametro.nombre
                        If objNombre.Name = "LayoutControlItem1" Then
                            MsgBox("ERROR")
                        End If
                        objNombre.Text = objParametro.label
                        Select Case objParametro.tipo
                            Case cls_config_parametro_.parametro_tipo.int '0
                                Dim Text As New DevExpress.XtraEditors.TextEdit
                                objNombre.Control = Text
                                Text.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
                                Text.Properties.Mask.UseMaskAsDisplayFormat = True
                                Text.Properties.Mask.EditMask = "##0"
                                Text.Text = objParametro.valor
                                ControlarModificacion(Text)
                            Case cls_config_parametro_.parametro_tipo.chk '1
                                Dim Check As New DevExpress.XtraEditors.CheckEdit
                                objNombre.TextVisible = False
                                objNombre.Control = Check
                                Check.Text = objParametro.label
                                Check.Checked = objParametro.valor
                                ControlarModificacion(Check)
                            Case cls_config_parametro_.parametro_tipo.col '2
                                Dim Color As New DevExpress.XtraEditors.ColorEdit
                                objNombre.Control = Color
                                Color.Text = objParametro.valor
                                ControlarModificacion(Color)
                            Case cls_config_parametro_.parametro_tipo.cbo '3
                                Dim Combo As New DevExpress.XtraEditors.ComboBoxEdit
                                Combo.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor
                                objNombre.Control = Combo
                                For i As Integer = 0 To Split(objParametro.valores, ",").Length - 1
                                    Dim obj As New Object
                                    Combo.Properties.Items.Add(Split(objParametro.valores, ",")(i).ToString)
                                    'Combo.Properties.Items(Combo.Properties.Items.Count - 1).tag = 1
                                Next
                                If IsNumeric(objParametro.valor) Then
                                    Combo.SelectedIndex = objParametro.valor
                                Else
                                    Combo.Text = objParametro.valor
                                End If
                                ControlarModificacion(Combo)
                            Case cls_config_parametro_.parametro_tipo.txt '4
                                Dim Text As New DevExpress.XtraEditors.TextEdit
                                objNombre.Control = Text
                                Text.Text = objParametro.valor
                                ControlarModificacion(Text)
                            Case cls_config_parametro_.parametro_tipo.dec '5 
                                Dim Text As New DevExpress.XtraEditors.TextEdit
                                objNombre.Control = Text
                                Text.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
                                Text.Properties.Mask.UseMaskAsDisplayFormat = True
                                Text.Properties.Mask.EditMask = "####0.00"
                                Text.Text = objParametro.valor
                                ControlarModificacion(Text)
                            Case cls_config_parametro_.parametro_tipo.dte '6
                                Dim Text As New DevExpress.XtraEditors.TextEdit
                                objNombre.Control = Text
                                Text.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime
                                Text.Properties.Mask.UseMaskAsDisplayFormat = True
                                Text.Properties.Mask.EditMask = "dd/MM/yyyy"
                                Text.Text = objParametro.valor
                                ControlarModificacion(Text)
                            Case cls_config_parametro_.parametro_tipo.pth  '7
                                Dim Text As New DevExpress.XtraEditors.ButtonEdit
                                AddHandler Text.ButtonClick, AddressOf pth_Click
                                objNombre.Control = Text
                                Text.Tag = objParametro.valores
                                Text.Properties.ReadOnly = True
                                Text.Text = objParametro.valor
                                ControlarModificacion(Text)
                            Case cls_config_parametro_.parametro_tipo.ctm ' 8
                                Dim Combo As New DevExpress.XtraEditors.LookUpEdit
                                objNombre.Control = Combo
                                Select Case objParametro.nombre
                                    Case "interfaz"
                                        Dim config_interfaz As New cls_config_interfaz_
                                        config_interfaz.CargarTodos()
                                        cargarCombo(Combo, config_interfaz.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "segurosCompañia"
                                        Dim proveedor_ As New cls_proveedor_
                                        proveedor_.CargarTodos(False)
                                        cargarCombo(Combo, proveedor_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "rubroPrincipal"
                                        Dim rubro_ As New cls_articulorubro_
                                        rubro_.CargarTodos()
                                        cargarCombo(Combo, rubro_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "polizaVencimiento"
                                        Dim tipovencimiento_ As New cls_tipovencimiento_
                                        tipovencimiento_.CargarTodos()
                                        cargarCombo(Combo, tipovencimiento_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "conceptoCtaCteCompra", "conceptoCtaCtePedido", "asientoconceptoCajaProveedor", "asientoconceptoCajaCliente", "asientoconceptoCajaPersonal"
                                        Dim asientoconcepto_ As New cls_asientoconcepto_
                                        asientoconcepto_.CargarTodos()
                                        cargarCombo(Combo, asientoconcepto_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "cajaasientoConceptoPedido", "cajaconceptoVentasR1", "cajaconceptoVentasR2", "cajaconceptoVentasR3"
                                        Dim cajaconcepto_ As New cls_cajaconcepto_
                                        cajaconcepto_.CargarXtipocaja(cls_caja_.enu_tipo.Reparto)
                                        cargarCombo(Combo, cajaconcepto_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "cajaasientoConceptoPedido", "cajaconceptoVentasM1", "cajaconceptoVentasM2", "cajaconceptoVentasM3"
                                        Dim cajaconcepto_ As New cls_cajaconcepto_
                                        cajaconcepto_.CargarXtipocaja(cls_caja_.enu_tipo.Mostrador)
                                        cargarCombo(Combo, cajaconcepto_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "cajaasientoConceptoPedido", "cajaconceptoAporteSocios", "cajaconceptoProveedores1", "cajaconceptoProveedores2", "cajaconceptoProveedores3", "cajaconceptoSueldos", "cajaconceptoRetiroSocios", "cajaasientoConceptoRecibo"
                                        Dim cajaconcepto_ As New cls_cajaconcepto_
                                        cajaconcepto_.CargarXtipocaja(cls_caja_.enu_tipo.General)
                                        cargarCombo(Combo, cajaconcepto_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "direccionFiscal"
                                        Dim tipodireccion_ As New cls_tipodireccion_
                                        tipodireccion_.CargarTodos()
                                        cargarCombo(Combo, tipodireccion_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "mailEmpresa"
                                        Dim tipoemail_ As New cls_tipoemail_
                                        tipoemail_.CargarTodos()
                                        cargarCombo(Combo, tipoemail_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "cargoInspector", "cargoEvaluador", "cargoSoldador"
                                        Dim cargo_ As New cls_cargo_
                                        cargo_.CargarTodos()
                                        cargarCombo(Combo, cargo_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "celularEmpresa"
                                        Dim tipotelefono_ As New cls_tipotelefono_
                                        tipotelefono_.CargarTodos()
                                        cargarCombo(Combo, tipotelefono_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "ensayo02unidadActividad", "ensayo02unidadGeneral", "unidadMilimetros", "unidadPulgadas"
                                        Dim unidad_ As New cls_unidad_
                                        unidad_.CargarTodos()
                                        cargarCombo(Combo, unidad_.Items)
                                        Combo.EditValue = CInt(objParametro.valor)
                                    Case "menuPorDefecto"
                                        Dim Combo1 As New DevExpress.XtraEditors.ComboBoxEdit
                                        Combo1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor
                                        cargarGrupos(Combo1)
                                        objNombre.Control = Combo1
                                        Combo1.EditValue = objParametro.valor
                                        ControlarModificacion(Combo1)
                                        Combo.Dispose()
                                End Select
                                If objParametro.valores = "0" Then
                                    If objParametro.valor = "0" Then
                                        Combo.EditValue = Nothing
                                    End If
                                    Combo.Properties.Buttons.Add(New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Delete))
                                    AddHandler Combo.ButtonClick, AddressOf ctm_ButtonClick
                                End If
                                ControlarModificacion(Combo)
                            Case cls_config_parametro_.parametro_tipo.fld
                                Dim Text As New DevExpress.XtraEditors.ButtonEdit
                                AddHandler Text.ButtonClick, AddressOf fld_Click
                                objNombre.Control = Text
                                Text.Properties.ReadOnly = True
                                Text.Text = objParametro.valor
                                ControlarModificacion(Text)
                            Case cls_config_parametro_.parametro_tipo.xmlReport
                                Dim Text As New DevExpress.XtraEditors.SimpleButton
                                AddHandler Text.Click, AddressOf xmlReport_Click
                                objNombre.Control = Text
                                Text.Text = "Iniciar Editor"
                                Text.ToolTipTitle = objParametro.valor
                                Text.Tag = objParametro.xml
                                ControlarModificacion(Text)
                        End Select
                    End If
                Next
            Next
        Next


       

        Dim objParametroEntorno As New cls_config_parametro_
        objParametroEntorno.Buscar("interfaz")
        entorno = objParametroEntorno.valor

        lcg_general.EndUpdate()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub cargarGrupos(ByRef prm_combo As DevExpress.XtraEditors.ComboBoxEdit)

        Dim STRgrupo As String = ""
        Dim STRsubGrupo As String = ""

        Dim obj_modulo_ As New cls_modulo_

        Dim primerGeneral As Boolean = False
        If obj_modulo_.CargarTodosOrdenado Then
            For Each modulo As cls_modulo_ In obj_modulo_.Items
                If Not IsNothing(modulo.config_modulo) Then
                    If modulo.config_modulo.activo Then
                        If STRgrupo <> modulo.grupo Then
                            If Not modulo.esroot Then
                                STRgrupo = modulo.grupo
                                If modulo.grupo <> "General" Then
                                    prm_combo.Properties.Items.Add(modulo.grupo)
                                End If
                            Else
                                If UsuarioLOG.esroot Then
                                    STRgrupo = modulo.grupo
                                    If modulo.grupo <> "General" Then
                                        prm_combo.Properties.Items.Add(modulo.grupo)
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            Next
        End If
    End Sub
    Private Sub xmlReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ABM As Object
        ABM = CType(Activator.CreateInstance(Type.GetType(Replace(Application.ProductName, " ", "_") & "." & sender.ToolTipTitle)), DevExpress.XtraReports.UI.XtraReport)
        If Not IsNothing(sender.tag) Then
            CType(ABM, DevExpress.XtraReports.UI.XtraReport).LoadLayoutFromXml(sender.control.tag)
        End If
        CType(ABM, DevExpress.XtraReports.UI.XtraReport).ShowDesignerDialog()
        Dim stream As New System.IO.MemoryStream()
        CType(ABM, DevExpress.XtraReports.UI.XtraReport).SaveLayoutToXml(stream)
        sender.tag = stream.GetBuffer()
    End Sub

    Private Sub pth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Dialog As New OpenFileDialog
        Dialog.Filter = sender.tag '"MP3 (*.mp3)|*.mp3|Archivos de sonido (*.wav)|*.wav|Todos los archivos (*.*)|*.*"
        If Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            sender.text = Dialog.FileName
        End If
    End Sub


    Private Sub fld_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Dialog As New FolderBrowserDialog
        If Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            sender.text = Dialog.SelectedPath
        End If
    End Sub


    Private Sub ctm_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs)
        If e.Button.Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Delete Then
            sender.editvalue = Nothing
        End If
    End Sub

#Region "Controlar Modificaciones"
    Public Sub ControlarModificaciones(ByRef coleccion As Object)
        For Each Group As Object In coleccion
            Select Case Group.GetType.ToString
                Case GetType(DevExpress.XtraLayout.LayoutControlGroup).ToString
                    For Each Item As Object In CType(Group, DevExpress.XtraLayout.LayoutControlGroup).Items
                        Select Case Item.GetType.ToString
                            Case GetType(DevExpress.XtraLayout.LayoutControlItem).ToString
                                ControlarModificacion(CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control)
                        End Select
                    Next
            End Select
        Next
    End Sub

    Public Sub ControlarModificacion(ByVal obj As Object)
        Select Case obj.GetType.Name
            Case GetType(DevExpress.XtraEditors.TextEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.TextEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.LookUpEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.LookUpEdit).EditValueChanging, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.ComboBoxEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.ComboBoxEdit).SelectedIndexChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.MemoEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.MemoEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraGrid.GridControl).Name
                Try
                    AddHandler CType(CType(obj, DevExpress.XtraGrid.GridControl).MainView, DevExpress.XtraGrid.Views.Grid.GridView).RowCountChanged, AddressOf obj_ValueChanged
                Catch ex As Exception
                End Try
                Try
                    AddHandler CType(CType(obj, DevExpress.XtraGrid.GridControl).MainView, DevExpress.XtraGrid.Views.Grid.GridView).CellValueChanged, AddressOf obj_ValueChanged
                Catch ex As Exception
                End Try
            Case GetType(DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.ImageListBoxControl).Name
                AddHandler CType(obj, DevExpress.XtraEditors.ImageListBoxControl).Validated, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.CheckEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.CheckEdit).CheckedChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.SimpleButton).Name
                AddHandler CType(obj, DevExpress.XtraEditors.SimpleButton).Click, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.PanelControl).Name
                For Each Control As Object In CType(obj, DevExpress.XtraEditors.PanelControl).Controls
                    ControlarModificacion(Control)
                Next
            Case GetType(DevExpress.XtraEditors.RadioGroup).Name
                AddHandler CType(obj, DevExpress.XtraEditors.RadioGroup).SelectedIndexChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.PictureEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.PictureEdit).DoubleClick, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraTreeList.TreeList).Name
                AddHandler CType(obj, DevExpress.XtraTreeList.TreeList).BeforeCheckNode, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.DateEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.DateEdit).DateTimeChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.ButtonEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.ButtonEdit).TextChanged, AddressOf obj_ValueChanged
        End Select
    End Sub

    Private Sub obj_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If sender.enabled Then
                flg_Modificado = True : btn_Guardar.Enabled = True : btn_guardarSalir.Enabled = True
            End If
        Catch ex As Exception
            flg_Modificado = True : btn_Guardar.Enabled = True : btn_guardarSalir.Enabled = True
        End Try
    End Sub

#End Region
    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.Close()
    End Sub

    Private Sub PanelControl1_DoubleClick(sender As Object, e As System.EventArgs) Handles PanelControl1.DoubleClick

    End Sub

    Private Sub btn_guardarSalir_Click(sender As System.Object, e As System.EventArgs) Handles btn_guardarSalir.Click
        btn_Guardar_Click(Nothing, Nothing)
        Me.Close()
    End Sub

End Class




